//u.js
//获取应用实例
const app = getApp();
var utils = require('../../utils/util.js');
Page({
  data: {
    result: '',
    data: '',
    codes: '',
    lunbo: '',
    appid: '', 
    hot_game:'',
    new_game: '',   
    hasUserInfo: false,
    showModal: true,
    user_id: ''
  },
 
  // 轮播图的点击跳转小程序
  lunbo: function (e) {
    var appid = e.currentTarget.dataset.index;
    utils.goWxProgress(appid);
  },
  goplay:function(e){
    var appid = e.currentTarget.dataset.index;
    var gameid = e.currentTarget.dataset.game;
    var user_id = this.data.user_id;
    utils.goWxProgress(appid);
    // 请求点击量
    wx.request({
      url: 'https://www.cacalife.cn/home/lecaca/gameview',
      methods: 'POST',
      data:{
        game_id: gameid,
        user_id: user_id
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res);
      }
    })
  },
  gostart: function (e) {
    var appid = e.currentTarget.dataset.index;
    var gameid = e.currentTarget.dataset.game;
    var user_id = this.data.user_id;
    utils.goWxProgress(appid);
    // 请求点击量
    wx.request({
      url: 'https://www.cacalife.cn/home/lecaca/gameview',
      methods: 'POST',
      data: {
        game_id: gameid,
        user_id: user_id
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res);
      }
    })
  },
  opensq: function(){
    var that = this;
    that.setData({
      showModal: false
    });
    wx.login({
      success: function (res) {
        var codes = res.code;
        wx.getUserInfo({
          success: res => {
            var data = JSON.parse(res.rawData);
            wx.setStorageSync('avatarUrl', data.avatarUrl);
            wx.setStorageSync('gender', data.gender);
            wx.setStorageSync('nickName', data.nickName);
            wx.request({
              url: 'https://www.cacalife.cn/home/lecaca/addusers',
              methods: 'POST',
              data: {
                'code': codes,
                'avatarUrl': data.avatarUrl,
                'gender': data.gender,
                'nickName': data.nickName
              },
              header: { 'content-type': 'application/json' },
              success: function (res) {
                console.log(res);
                that.setData({
                  openid: res.data.user_id
                })
                wx.setStorageSync('user_id', res.data.user_id)
              },
              fail: function (res) {
                console.log(res)
              }
            })
          }
        })
      }
    })
  },
  onLoad: function () {
    if (this.data.user_id){
      this.setData({
        showModal: false
      })
   }else{
      this.setData({
        showModal: true
      })
   }
    wx.getStorage({
      key: 'user_id',
      success: function (res) {
        if(res.data){
          wx.request({
            url: 'https://www.cacalife.cn/home/lecaca/count_num',
            methods: 'POST',
            data: {
              user_id: res.data
            },
            header: { 'content-type': 'application/json' }
          })
        }
        that.setData({
          user_id: res.data
        })
      }
    })
    var that = this;
    console.log()
    wx.request({
      url: 'https://www.cacalife.cn/home/lecaca/games',
      methods: 'POST',
      data:{
        user_id: that.data.user_id
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res)
        var lunbo = res.data.lunbo;
        var hot_game = res.data.hot_game;
        var new_game = res.data.new_game;
       
        that.setData({
          lunbo: lunbo,
          hot_game: hot_game,
          new_game: new_game
        })
      }
    })

  }
})
